/**
 *  
 */
package cs.rsa.ts14.hotel;

import java.util.*;


import cs.rsa.ts14.framework.LineType;
import cs.rsa.ts14.framework.LineTypeClassifierStrategy;

/**
 * Implementation for the LineTypeClassifierStrategy
 * @author Hotel
 *
 */
public class StandardLineTypeClassifierStrategy implements
		LineTypeClassifierStrategy {
	

	private static Map<LineType, String> PatternMap;
	static { 
		PatternMap =  new HashMap<LineType, String>();
		PatternMap.put(LineType.WEEK_SPECIFICATION, "^Week\\s+([1-5]+[0-2]+|[1-9])\\s+:\\s+([0-5]\\s+:\\s+[0]|[0-4]\\s+:\\s+[1]|[0-3]\\s+:\\s+[2]|[0-2]\\s+:\\s+[3]|[0-1]\\s+:\\s+[4]|[0]\\s+:\\s+[5])\\s*");
		PatternMap.put(LineType.EMPTY_LINE, "^\\s*");
		PatternMap.put(LineType.COMMENT_LINE, "^#\\s*");
		PatternMap.put(LineType.ASSIGNMENT_LINE, "^HoursOvertime\\s+=\\s\\d+[.]*[0|5]");
		PatternMap.put(LineType.WORK_SPECIFICATION, "^\\s+[A-Za-z]\\S+\\s+([A-Za-z]\\S+|-)\\s+\\d[.](0|5)");
		PatternMap.put(LineType.WEEKDAY_SPECIFICATION, "^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\\s+(Bi|Ca|Pu|Tr|No|Ho)\\s*.*");
		
	}
	

	 LineType lastSeen;
	 String   lastError;

	@Override
	public LineType classify(String line) {
		 lastSeen = LineType.INVALID_LINE;
		 lastError = "unknown error";
		 
		 // create a set of keys to iterator
		Iterator<LineType> KeySetIterator = PatternMap.keySet().iterator();
		
		while(KeySetIterator.hasNext())
		{
			LineType key = KeySetIterator.next();
			
			boolean okRes = line.matches(PatternMap.get(key));
			
			if(okRes)
			{
				lastSeen = key;
				lastError = "no error";	
		
				break;
			}
			else
			{
				lastSeen = LineType.INVALID_LINE;
				lastError = String.format("Parse error on linetype: %s",  key.toString());
			
			}

			
			
		}
		
		return lastSeen;
	}


	@Override
	public String lastError() {
      return lastError;		

	}
	
	


}
